/**
 * ESLint 配置
 * https://eslint.org/docs/latest/use/configure/
 */
import js from "@eslint/js"
import vue from "eslint-plugin-vue"

export default [
  // JavaScript 推荐配置
  js.configs.recommended,

  // Vue 3 推荐配置
  ...vue.configs["flat/essential"],

  {
    languageOptions: {
      ecmaVersion: "latest",
      sourceType: "module",
      globals: {
        // Node.js 环境
        process: "readonly",
        Buffer: "readonly",
        __dirname: "readonly",
        __filename: "readonly",

        // 浏览器环境
        window: "readonly",
        document: "readonly",
        console: "readonly",
        localStorage: "readonly",
        sessionStorage: "readonly",
        setTimeout: "readonly",
        clearTimeout: "readonly",
        setInterval: "readonly",
        clearInterval: "readonly",
        navigator: "readonly",
        fetch: "readonly",
        FormData: "readonly",
        Blob: "readonly",
        File: "readonly",
        FileReader: "readonly",
        URLSearchParams: "readonly",
        URL: "readonly",
        TextDecoder: "readonly",
        TextEncoder: "readonly",
        btoa: "readonly",
        atob: "readonly",
        performance: "readonly",
        Node: "readonly",
        Element: "readonly",
        Event: "readonly",
        CustomEvent: "readonly",

        // Vue 3 Composition API 编译器宏
        defineProps: "readonly",
        defineEmits: "readonly",
        defineExpose: "readonly",
        withDefaults: "readonly",

        // Vite
        import: "readonly"
      }
    },

    rules: {
      // 控制台和调试
      "no-console": process.env.NODE_ENV === "production" ? "warn" : "off",
      "no-debugger": process.env.NODE_ENV === "production" ? "warn" : "off",

      // Vue 规则
      "vue/multi-word-component-names": "off", // 允许单词组件名
      "vue/no-unused-vars": "warn", // 未使用的变量警告
      "vue/no-v-html": "warn", // v-html 使用警告（XSS 风险）
      "vue/require-default-prop": "off", // 不强制 prop 默认值
      "vue/require-prop-types": "warn", // prop 类型定义警告
      "vue/no-multiple-template-root": "off", // 允许多个根元素（Vue 3）

      // JavaScript 规则
      "no-unused-vars": ["warn", { argsIgnorePattern: "^_", varsIgnorePattern: "^_" }], // 未使用变量警告
      "no-undef": "error", // 未定义变量错误
      "prefer-const": "warn", // 优先使用 const
      "no-var": "warn", // 不使用 var
      "no-prototype-builtins": "off",
      "no-case-declarations": "off",

      // 代码风格（由 Prettier 处理，这里关闭）
      quotes: "off",
      semi: "off",
      "comma-dangle": "off",
      indent: "off",
      "no-trailing-spaces": "off",
      "eol-last": "off"
    }
  },

  {
    // 测试文件配置
    files: ["**/__tests__/**/*.{js,ts}", "**/*.{test,spec}.{js,ts}"],
    languageOptions: {
      globals: {
        describe: "readonly",
        test: "readonly",
        it: "readonly",
        expect: "readonly",
        beforeEach: "readonly",
        afterEach: "readonly",
        beforeAll: "readonly",
        afterAll: "readonly",
        jest: "readonly",
        vi: "readonly"
      }
    }
  },

  {
    // 忽略文件
    ignores: [
      "**/node_modules/**",
      "**/dist/**",
      "**/build/**",
      "**/.vite/**",
      "**/coverage/**",
      "**/*.min.js",
      "**/public/**"
    ]
  }
]
